#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
selenium基类
本文件存放了selenium基类的封装方法
"""
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from config.conf import cm
from config.readIni import ini
from utils.times import sleep
from utils.logger import log
import allure
from utils.yamlReader import Readyaml


class WebPage():
    """selenium基类"""

    def __init__(self, driver):
        # self.driver = webdriver.Edge()
        self.driver = driver
        self.wait = WebDriverWait(self.driver, 5,0.5)
        self.baseUrl=ini.base_url
        self.path='/'
        self.eles=Readyaml(cm.ELEMENT_DIR)


    def open(self):
        """打开网址并验证"""
        # self.driver.maximize_window()
        self.driver.set_page_load_timeout(60)
        url = self.baseUrl+self.path
        try:
            self.driver.get(url)
            self.driver.implicitly_wait(10)
            log.info("打开网页：%s" % url)
        except TimeoutException:
            raise TimeoutException("打开%s超时请检查网络或网址服务器" % url)

    @staticmethod
    def element_locator(func, locator=None):
        """元素定位器"""
        name, value = locator.split('==')
        return func(cm.LOCATE_MODE[name], value)

    def find_element(self, locator):
        """寻找单个元素"""
        try:
            return WebPage.element_locator(lambda *args: self.wait.until(
            EC.presence_of_element_located(args)),locator)
        except TimeoutException:
            raise TimeoutException(f'找不到元素{locator}')

    def find_elements(self, locator):
        """查找多个相同的元素"""
        return WebPage.element_locator(lambda *args: self.wait.until(
            EC.presence_of_all_elements_located(args)), locator)

    def elements_num(self, locator):
        """获取相同元素的个数"""
        number = len(self.find_elements(locator))
        log.info("相同元素：{}".format((locator, number)))
        return number

    def element_text(self, locator):
        """获取当前的text"""
        _text = self.find_element(locator).text
        log.info("获取文本：{}".format(_text))
        return _text

    def is_displayed(self,dictLocator):
        return self.find_element(dictLocator.loc).is_displayed()

    @property
    def get_source(self):
        """获取页面源代码"""
        return self.driver.page_source

    def refresh(self):
        """刷新页面F5"""
        self.driver.refresh()
        self.driver.implicitly_wait(30)

    def input_text(self, dictLocator, txt):
        """输入(输入前先清空)"""
        # sleep(0.5)
        ele = self.find_element(dictLocator.loc)
        ele.clear()
        with allure.step(f'步骤：在 {dictLocator.desc} 输入：{txt}'):
            ele.send_keys(txt)
        log.info(f'在 {dictLocator.desc} 输入：{txt}')

    def click(self, dictLocator):
        """点击"""
        ele=self.find_element(dictLocator.loc)
        sleep()
        with allure.step('步骤：点击 %s' % dictLocator.desc):
            ele.click()
        log.info("点击元素：{}".format(dictLocator.desc))


if __name__ == "__main__":
    pass
